precision highp float;
precision highp int;

// NOTE
// https://godotshaders.com/shader/magnifier-shader/

uniform sampler2D sTexture;

// Screen resolution.
uniform vec2 screen_res;

// Target position as center of magnifilter.
uniform vec2 center_pos;

// Variable to describe how magnify. [0, 1)
uniform float magnification;

// Radius to adopt magnification.
uniform float radius;

// Line width of magnifier circle.
uniform float circle_line_width;

// Line color of magnifier circle.
uniform vec3 circle_line_color;

// In OpenGL ES 2.0, use 'varying' instead of 'in'
varying vec2 vTextureCoord;

// output colour for the fragment
// OpenGL ES 2.0 doesn't have explicit 'out' variables
// The output color is written to 'gl_FragColor'

void main()
{
    vec2 center_uv = center_pos / screen_res;

    // Compute proportional vTextureCoord to magnify.
    vec2 magnifiled_uv = mix(vTextureCoord, center_uv, magnification);

    vec2 frag_coord = gl_FragCoord.xy;
    frag_coord.y = screen_res.y - frag_coord.y;

    float d = length(frag_coord - center_pos);
    if (d > radius)
    {
        magnifiled_uv = vTextureCoord;
    }

    if (radius < d && d <= (radius + circle_line_width)) {
        gl_FragColor.rgb = circle_line_color;
    }
    else {
        vec4 color = texture2D(sTexture, magnifiled_uv);
        gl_FragColor.rgb = color.rgb;
    }

    gl_FragColor.a = 1.0;
}